<?php
	drupal_set_title("Administration Menu");
    require_once ("prepend.inc");
	require_once ("qform/QAutocompleteTextBox.inc");

	// Define the Qform with all our Qcontrols
	class FrontFormAdmin extends QForm {
		// Local declarations of our Qcontrols
		protected $txtSearch;
		protected $btnButton;
		protected $btnBlogEntry;
		protected $btnSettings;
		protected $btnKb;
		protected $btnReports;
		protected $btnMgrContact;
		protected $btnManageColleges;
		protected $lblResult;

		// Initialize our Controls during the Form Creation process
		protected function Form_Create() {
			// Define the Label
			$this->txtSearch = new QAutocompleteTextBox($this);
			$this->txtSearch->Width = "400px";
			$this->txtSearch->CssClass = 'informtext';
			$this->txtSearch->SetCallback('autoCompleteAction');

			// Definte the Button
			$this->btnButton = new QButton($this);
			$this->btnButton->Text = 'Search';
			$this->btnButton->CssClass = 'informsubmit';

			$this->btnButton->AddAction(new QClickEvent(), new QServerAction('btnButton_Click'));
			$this->txtSearch->AddAction(new QEnterKeyEvent(), new QServerAction('btnButton_Click'));
			
			$this->lblResult = new QLabel($this);
			
			$this->btnBlogEntry_Create();
			$this->btnSettings_Create();
			$this->btnKb_Create();
			$this->btnReports_Create();
			$this->btnMgrContact_Create();
			$this->btnManageColleges_Create();
		}

		public function autoCompleteAction($strSearch) {
			$results = CcoCollege::LoadArraySearchName($strSearch,'',10);
			foreach($results as $result) {
				$this->txtSearch->AddItem($result->Name,$result->Name);
			}
		}
		
		// Protected Create Methods
		// Create and Setup btnBlogEntry
		protected function btnBlogEntry_Create() {
			$this->btnBlogEntry = new QButton($this);
			$this->btnBlogEntry->Text = 'New Blog Entry';
			$this->btnBlogEntry->CssClass = 'informsubmit';
			$this->btnBlogEntry->AddAction(new QClickEvent(), new QAjaxAction('btnBlogEntry_Click'));
		}

		// Create and Setup btnSettings
		protected function btnSettings_Create() {
			$this->btnSettings = new QButton($this);
			$this->btnSettings->Text = 'Settings';
			$this->btnSettings->CssClass = 'informsubmit';
			$this->btnSettings->AddAction(new QClickEvent(), new QAjaxAction('btnSettings_Click'));
		}

		// Create and Setup btnKb
		protected function btnKb_Create() {
			$this->btnKb = new QButton($this);
			$this->btnKb->Text = 'Knowledge Base';
			$this->btnKb->CssClass = 'informsubmit';
			$this->btnKb->AddAction(new QClickEvent(), new QAjaxAction('btnKb_Click'));
		}

		// Create and Setup btnReports
		protected function btnReports_Create() {
			$this->btnReports = new QButton($this);
			$this->btnReports->Text = 'Reports';
			$this->btnReports->CssClass = 'informsubmit';
			$this->btnReports->AddAction(new QClickEvent(), new QAjaxAction('btnReports_Click'));
		}

		// Create and Setup btnMgrContact
		protected function btnMgrContact_Create() {
			$this->btnMgrContact = new QButton($this);
			$this->btnMgrContact->Text = 'Store';
			$this->btnMgrContact->CssClass = 'informsubmit';
			$this->btnMgrContact->AddAction(new QClickEvent(), new QAjaxAction('btnMgrContact_Click'));
		}

		// Create and Setup btnManageColleges
		protected function btnManageColleges_Create() {
			$this->btnManageColleges = new QButton($this);
			$this->btnManageColleges->Text = 'Manage Colleges';
			$this->btnManageColleges->CssClass = 'informsubmit';
			$this->btnManageColleges->AddAction(new QClickEvent(), new QAjaxAction('btnManageColleges_Click'));
		}

		protected function btnBlogEntry_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url("node/add/blog"));
		}

		protected function btnSettings_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url(QMODULE_NAME."/Settings"));
		}

		protected function btnKb_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url(QMODULE_NAME."/Knowledge-Base"));
		}

		protected function btnReports_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url(QMODULE_NAME."/Reports"));
		}

		protected function btnMgrContact_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url("admin/store"));
		}

		protected function btnManageColleges_Click($strFormId, $strControlId, $strParameter) {
			QApplication::Redirect(qmodule_url(QMODULE_NAME."/Manage-Colleges"));
		}

		protected function btnButton_Click($strFormId, $strControlId, $strParameter) {
			$strSearchZip = "";
			if ( preg_match('/^\d{5}$/',$this->txtSearch->Text) ) {
				$strSearchZip = $this->txtSearch->Text;
			}else {
				$arrNameMatches = CcoCollege::LoadArraySearchName($this->txtSearch->Text);
				if ( count($arrNameMatches) > 0 ) {
					$match = null;
					foreach((array)$arrNameMatches as $k => $match) {
						if($match->Name == $this->txtSearch->Text) {
							$match = $k;
							break;
						}
					}
							
					if(!is_null($match))
						$strSearchZip = $arrNameMatches[$match]->Zip;
					else
						$strSearchZip = $arrNameMatches[0]->Zip;
				}
			}
			QApplication::Redirect(qmodule_url(QMODULE_NAME."/Search-Results/".$strSearchZip));
		}
		
		protected function Form_Load() {
			// Let's always clear the Result label
			$this->lblResult->Text = '';
		}
	}

	FrontFormAdmin::Run('FrontFormAdmin', qmodule_tmpl_path('FrontFormAdmin.php.inc'));
	return;
?>
